//
//  ShoppingMallViewController.m
//  快圈
//
//  Created by ym on 16/9/10.
//  Copyright © 2016年 ym. All rights reserved.
//

#import "ShoppingMallViewController.h"
#import "ListTableViewCell.h"
#import "HuoDongShowTableViewCell.h"
#import "HeZuoNamedTableViewCell.h"
#import "ShangPinListTableViewCell.h"

@interface ShoppingMallViewController ()<UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *ImageDataArr;
@end

@implementation ShoppingMallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商城";
    [self data];
    [self initUI];


}
- (void)initUI{
    UIView *backView = [[UIView alloc] init];
    backView.frame = CGRectMake(0, 0, WIDTH, 160 + 40);
    
    UIScrollView *scView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 40, WIDTH, 160)];
    [backView addSubview:scView];
    scView.pagingEnabled = YES;
    scView.contentSize = CGSizeMake(WIDTH * self.ImageDataArr.count, 0);
    scView.showsVerticalScrollIndicator = NO;
    scView.showsHorizontalScrollIndicator = NO;
    scView.delegate = self;
    scView.tag = 20001;
    for (NSInteger i = 0 ; i < self.ImageDataArr.count; i ++) {
        UIImageView *a = [[UIImageView alloc] initWithFrame:CGRectMake(WIDTH * i, 0, WIDTH, scView.height)];
        [scView addSubview:a];
        a.image = self.ImageDataArr[i];
    }
    
    UIPageControl *pageControl =
    [[UIPageControl alloc] init];
    pageControl.frame = CGRectMake(WIDTH/2 - 50, 160, 100, 40);
//        pageControl.center = scView.center;
//    pageControl.backgroundColor = [UIColor orangeColor];
    pageControl.tag = 2000;
    [backView addSubview:pageControl];
    pageControl.numberOfPages = self.ImageDataArr.count;
    pageControl.currentPage = 0;
    pageControl.currentPageIndicatorTintColor = [UIColor colorWithRed:26/255.0 green:188/255.0 blue:156/255.0 alpha:1];
    // 未选中圆点颜色
    pageControl.pageIndicatorTintColor = [UIColor whiteColor];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64) style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
//    self.tableView.tableHeaderView = backView;
    self.tableView.separatorStyle = NO;
    self.tableView.showsHorizontalScrollIndicator = NO;
    self.tableView.showsVerticalScrollIndicator = NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"ListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ListTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"HuoDongShowTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HuoDongShowTableViewCell"];
      [self.tableView registerNib:[UINib nibWithNibName:@"HeZuoNamedTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HeZuoNamedTableViewCell"];
     [self.tableView registerNib:[UINib nibWithNibName:@"ShangPinListTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"ShangPinListTableViewCell"];
  
    
    UIImageView *leftView = [[UIImageView alloc] init];
    leftView.image = [UIImage imageNamed:@"find"];
    leftView.frame = CGRectMake(0, 0, 20, 20);
    
    UIImageView *footer = [[UIImageView alloc] init];
    footer.frame = CGRectMake(0, 0, WIDTH, 15);
    footer.backgroundColor = BackColour;
    footer.image  = [UIImage imageNamed:@"asasasa"];
    self.tableView.tableFooterView = [self footerView];
    UIView *topView = [[UIView alloc] init];
    topView.frame = CGRectMake(0, 0, WIDTH, 40);
    topView.backgroundColor = TempGreen;
    [backView addSubview:topView];
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(15, 7, WIDTH - 30, 30-4)];
    [topView addSubview:textField];
    textField.placeholder = @"  商品、品牌";
    textField.backgroundColor = [UIColor whiteColor];
    textField.layer.masksToBounds = YES;
    textField.layer.cornerRadius = 5;
    textField.leftView =leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    self.tableView.tableHeaderView = backView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 3) {
        return 10;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ListTableViewCell"];
        cell.backgroundColor = RGBACOLOR(55, 53, 66, 1);
        
        return cell;
    }else if (indexPath.section == 1){
        HuoDongShowTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HuoDongShowTableViewCell"];
        cell.backgroundColor = RGBACOLOR(55, 53, 66, 1);
        return cell;
        
    }else if (indexPath.section == 2){
        // 失误
        HeZuoNamedTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HeZuoNamedTableViewCell"];
        cell.backgroundColor = RGBACOLOR(55, 53, 66, 1);
        return cell;
        
    }else if (indexPath.section == 3){
        ShangPinListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ShangPinListTableViewCell"];
        cell.backgroundColor = RGBACOLOR(55, 53, 66, 1);
        return cell;
    }
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 125;
    }else if (indexPath.section == 1){
        return 200;
    }else if( indexPath.section == 2){
        return 50;
    }else if (indexPath.section == 3){
        return 210;
    }
    return 0;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)data{
    self.ImageDataArr = [NSMutableArray array];
    for (NSInteger i = 0 ; i <5; i ++) {
        UIImage *a = [[UIImage alloc] init];
        a = [UIImage imageNamed:@"tu"];
        [self.ImageDataArr addObject:a];
    }
    
    
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
 
    if (scrollView.tag == 20001) {
        if (scrollView.contentOffset.x / WIDTH == self.ImageDataArr.count) {
            scrollView.contentOffset = CGPointZero;
        }
        // 设置当前页码
        UIPageControl *pc = (UIPageControl *)[self.view viewWithTag:2000];
        pc.currentPage = scrollView.contentOffset.x / WIDTH;
    }
 
}

@end
